package org.vison.wonfu.framework.codestyle.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 检查器配置类，负责加载和解析YAML配置文件
 */
public class CheckerConfig {
    private final Map<String, RuleConfig> rules = new HashMap<>();
    private String basePackage = "com.example.checker";
    
    // 从文件加载配置
    public static CheckerConfig loadFromFile(String filePath) throws IOException {
        ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
        return mapper.readValue(new File(filePath), CheckerConfig.class);
    }
    
    // 获取规则配置
    public RuleConfig getRuleConfig(String ruleId) {
        return rules.getOrDefault(ruleId, new RuleConfig(true));
    }
    
    // 检查规则是否启用
    public boolean isRuleEnabled(String ruleId) {
        RuleConfig config = getRuleConfig(ruleId);
        return config.isEnabled();
    }
    
    // 获取规则的参数配置
    public <T> T getRuleProperty(String ruleId, String propertyName, Class<T> type, T defaultValue) {
        RuleConfig config = getRuleConfig(ruleId);
        Object value = config.getProperties().get(propertyName);
        
        if (value == null) {
            return defaultValue;
        }
        
        try {
            return type.cast(value);
        } catch (ClassCastException e) {
            return defaultValue;
        }
    }
    
    // Getters and Setters
    public Map<String, RuleConfig> getRules() {
        return rules;
    }
    
    public String getBasePackage() {
        return basePackage;
    }
    
    public void setBasePackage(String basePackage) {
        this.basePackage = basePackage;
    }
    
    /**
     * 单个规则的配置
     */
    public static class RuleConfig {
        private boolean enabled = true;
        private boolean strict = false;
        private Map<String, Object> properties = new HashMap<>();
        
        public RuleConfig() {}
        
        public RuleConfig(boolean enabled) {
            this.enabled = enabled;
        }
        
        public boolean isEnabled() {
            return enabled;
        }

        public boolean isStrict() {
            return strict;
        }

        public void setStrict(boolean strict) {
            this.strict = strict;
        }
        
        public void setEnabled(boolean enabled) {
            this.enabled = enabled;
        }
        
        public Map<String, Object> getProperties() {
            return properties;
        }
        
        public void setProperties(Map<String, Object> properties) {
            this.properties = properties;
        }
    }
}
